#include "attendanceregisterwidget.h"
#include "ui_attendanceregisterwidget.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include "loginwidget.h"
#include <QDateTime>

AttendanceRegisterWidget::AttendanceRegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AttendanceRegisterWidget)
{
    ui->setupUi(this);
    initWidget();
}

AttendanceRegisterWidget::~AttendanceRegisterWidget()
{
    delete ui;
}

/**
 * @brief 初始化窗体
 */
void AttendanceRegisterWidget::initWidget()
{
    this->setWindowTitle("出勤登记");
}

/**
 * @brief 上班登记
 */
void AttendanceRegisterWidget::on_btn_onDuty_clicked()
{
    QSqlQuery query;
    query.prepare("INSERT INTO attendance (userId, time, type) VALUES(:userId, :time, :type)");
    query.bindValue(":userId", LoginWidget::userId); //登记人ID
    QString dataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");//获取当前时间
    query.bindValue(":time", dataTime); //登记时间
    query.bindValue(":type", 1); //登记类型：上班
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        QMessageBox::information(this, tr("提示"), tr("登记成功"), QMessageBox::Ok);
    }
}

/**
 * @brief 下班登记
 */
void AttendanceRegisterWidget::on_btn_offDuty_clicked()
{
    QSqlQuery query;
    query.prepare("INSERT INTO attendance (userId, time, type) VALUES(:userId, :time, :type)");
    query.bindValue(":userId", LoginWidget::userId); //登记人ID
    QString dataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");//获取当前时间
    query.bindValue(":time", dataTime); //登记时间
    query.bindValue(":type", 2); //登记类型：下班
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        QMessageBox::information(this, tr("提示"), tr("登记成功"), QMessageBox::Ok);
    }
}
